package com.bruce.shop.dao.impl;

import com.bruce.shop.commons.utils.DBUtils;
import com.bruce.shop.dao.ProductTypeDao;
import com.bruce.shop.entity.ProductType;

import java.util.List;

/**
 * @author BruceXia
 * @version 1.0.0
 * @description:
 * @date 2022/9/1823:02
 */
public class ProductTypeDaoImpl implements ProductTypeDao {
    /**
     * 前端传入pid的值（-1），然后在Dao层通过这个pid查出其下所有的商品类型.
     * 在这里本质上是为了查出所有商品类型的大类给前端,让用户也能添加所有大类
     *  中的其余小类的商品类型
     * @param pid
     * @return
     */
    @Override
    public List<ProductType> getProductListByPid(int pid) {
        String sql = "select * from t_product_type where pid = ?";
        return DBUtils.selectAll(sql, ProductType.class, pid);
    }

    @Override
    public Integer insert(ProductType productType) {
        String sql = "insert into t_product_type (name,icon,pid) values(?,?,?)";
        return DBUtils.write(sql, productType.getName(), productType.getIcon(), productType.getPid());
    }

    @Override
    public Integer update(ProductType productType) {
        String sql = "update t_product_type set name = ? ,pid = ? where id = ?";
        return DBUtils.write(sql, productType.getName(), productType.getPid(), productType.getId());
    }

    @Override
    public Integer deleteById(Integer id) {
        String sql = "delete from t_product_type where id = ?";
        return DBUtils.write(sql, id);
    }

    @Override
    public ProductType selectById(Integer id) {
        String sql = "select * from  t_product_type where id = ?";
        return DBUtils.selectOne(sql, ProductType.class, id);
    }

    @Override
    public List<ProductType> list() {
        String sql = "select * from  t_product_type";
        return DBUtils.selectAll(sql, ProductType.class);
    }

    @Override
    public Integer count() {
        String sql = "select count(1) from  t_product_type";
        return DBUtils.readCount(sql);
    }

    @Override
    public List<ProductType> list(Integer size, Integer index) {
        String sql = "select * from  t_product_type limit ?,?";
        return DBUtils.selectAll(sql, ProductType.class, index, size);
    }
}
